<?php
// ini_set('display_errors', 1); 
error_reporting(E_ALL);
class JobseekersController extends AppController {
    //var $components = array('Auth');
    var $name = 'Jobseekers';
	var $helpers = array('Html', 'Form');
    function get_count()
    {
        $count = $this->Jobseeker->find('count',array('conditions'=>array('Jobseeker.status'=>1)));
        return $count;
    }

    function checkname()
    {
        $this->layout = 'ajax';
        $this->Jobseeker->recursive = -1; 
        $pass=$this->Jobseeker->find('count',array( 'conditions'=> array( 'Jobseeker.username'=>$this->data['Jobseeker']['username'])));
        $this->set('pass',$pass);
    }

    function complete_personal()
    {
        $query = "SELECT address FROM `jobseekers` WHERE id=".$this->Session->read('User.id');
        $data = $this->Jobseeker->query($query);
        if($data[0]['jobseekers']['address']!='')
        { return true; }
        else
        { return false; }
    }
    
    function complete_all()
    {
        $query = "SELECT address FROM `jobseekers` WHERE id=".$this->Session->read('User.id');
        $data = $this->Jobseeker->query($query);
        $query = "SELECT academic_id FROM `resumes` WHERE jobseeker_id=".$this->Session->read('User.id');
        $data2 = $this->Jobseeker->query($query);  
         if( $data[0]['jobseekers']['address']!='' && $data2[0]['resumes']['academic_id']!=0 )
        { return true; }
        else
        { return false; }
    }
    
    function get_resume($jobseeker_id = null)
    {
        $this->Jobseeker->recursive=1;
        $data = $this->Jobseeker->read(null,  $jobseeker_id);
/*        $this->Jobseeker->Religion->recursive=-1;
        $religion = $this->Jobseeker->Religion->find('all',array('conditions'=>array('Religion.id'=>$data['Jobseeker']['religion_id'])));
        $data['religion_name'] = $religion[0]['Religion']['name'];
*/        $this->Jobseeker->Resume->Academic->recursive=-1;
        $academic = $this->Jobseeker->Resume->Academic->find('all',array('conditions'=>array('Academic.id'=>$data['Resume']['academic_id'])));
        $data['academic_name'] = $academic[0]['Academic']['name'];
        return $data;    
    }
    
    function preview_resume($id = null) 
    {
        $this->layout = 'resume';
        $this->Jobseeker->recursive=0;
        $data = $this->Jobseeker->find('first',array('conditions'=>array('Jobseeker.status'=>1,'Jobseeker.id'=>$id,'Resume.academic_id <>'=>'0'))); 
        if(empty($data))
        { 
            $this->Session->setFlash(__('Invalid ID', true)); 
            $this->redirect(array('controller'=>'jobs', 'action'=>'index'),null, true);        
        }
        else
        { 
            if( $this->Session->read('User.type')=='jobseeker' || $this->Session->read('User.type')=='employer')
            {
                if($this->Session->read('User.type')=='jobseeker')
                {
                    if($this->Session->read('User.id')!=$id)
                    { 
                        $this->Session->setFlash(__('Forbidden Link', true)); 
                        $this->redirect(array('controller'=>'jobs', 'action'=>'index'),null, true);
                    }
                }
                else
                {
                    $temp['id'] = $id;
                    $temp['views'] = $data['Resume']['views']+1;
                    $query = "UPDATE `resumes` SET `views` = ".$temp['views']." WHERE `id` =".$temp['id'];
                    $this->Jobseeker->query($query);
                }
            }
            else
            { 
                $this->Session->setFlash(__('Forbidden Link', true)); 
                $this->redirect(array('controller'=>'jobs', 'action'=>'index'),null, true);
            }            
            $this->set('id',$id); 
        }
    }    

    function signup()
    {
        $this->set('choose1',3);
        
        
        if( $this->Session->read('User.type')=='jobseeker' )
        { $this->redirect(array('controller'=>'jobseekers', 'action'=>'index'),null, true); }        
        if( $this->Session->read('User.type')=='employer' )
        { $this->redirect(array('controller'=>'employers', 'action'=>'index'),null, true); }
                
        if(!empty($this->data))
        {
            $pass=$this->Jobseeker->find('count',array( 'conditions'=> array( 'or' => array('Jobseeker.username'=>$this->data['Jobseeker']['username'],'Jobseeker.email'=>$this->data['Jobseeker']['email'] ))));
            if($pass==0)
            {
                $this->Jobseeker->create();
                $this->data['Jobseeker']['codes'] = String::uuid();
                $this->data['Jobseeker']['status'] = 1;
                $this->data['Jobseeker']['city_id'] = $_POST['data']['Jobseeker']['City'];
                if ($this->Jobseeker->save($this->data)) 
                {
                    $newId = $this->Jobseeker->id;
                    
                    $to = $this->data['Jobseeker']['email'];
					include("PHPMailer/class.phpmailer.php");
					include("PHPMailer/class.smtp.php"); // note, this is optional - gets called from main class if not already loaded

					$mail             = new PHPMailer();

					$mail->IsSMTP();
					$mail->SMTPAuth   = true;                  // enable SMTP authentication
					$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
					$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
					$mail->Port       = 465;                   // set the SMTP port

					$mail->Username   = "itjobz.indo@gmail.com";  // GMAIL username
					$mail->Password   = "1qaz2wsx3ec";            // GMAIL password

					$mail->From       = "itjobz.indo@gmail.com";
					$mail->FromName   = "Webmaster";
					$mail->Subject    = "Jobseeker Member Activation";
					$mail->AltBody    = "Jobseeker Member Activation"; //Text Body
					$mail->WordWrap   = 50; // set word wrap
					$link = "http://www.itjobz.jux.in/jobseekers/verification/".$newId."/".$this->data['Jobseeker']['codes'];
                    $body = " 
                                Please click this link to activate your jobseeker member account<br>
                                $link
                            ";   
					$mail->MsgHTML($body);

					$mail->AddReplyTo("itjobz.indo@gmail.com","Webmaster");

					$mail->AddAddress($to,"First Last");

					$mail->IsHTML(true); // send as HTML

					$mail->Send();
					// if(!$mail->Send()) {
					  // echo "Mailer Error: " . $mail->ErrorInfo;
					// } else {
					  // echo "Message has been sent";
					// }
					
                    $this->data['Assistant']['id']=$newId;                        
                    $this->data['Assistant']['jobseeker_id']=$newId;
                    $this->Jobseeker->Assistant->create();
                    $this->Jobseeker->Assistant->save($this->data);
                    
                    $this->data['Resume']['id']=$newId;
                    $this->data['Resume']['jobseeker_id']=$newId;
                    $this->Jobseeker->Resume->create();
                    $this->Jobseeker->Resume->save($this->data);
                }
                $this->Session->setFlash(__('Congratulations, your account has been created. Please verify your email to activate your account', true));
                $this->set('sukses',1);
				// print_r($this->data);
				// echo ;
            }
            else
            {
                $this->Session->setFlash(__('There is an error, your username or email has been used', true));
            }
        }   
        $cities = $this->Jobseeker->City->find('list',array('order'=>'City.name')); 
        $this->set('cities',$cities); 
        
        // $this->Jobseeker->City->recursive=-1;
        // $cities2	= $this->Jobseeker->City->find('all',array('order'=>'City.name')); 
		$skills		= $this->Jobseeker->Skill->find('list');
        $this->set('skills',$skills); 
        // $this->set('cities2',$cities2);
    }
    
    function verification( $id = null, $codes = null )
    {
        if( !empty($id) && !empty($codes) )
        {
            $this->Jobseeker->recursive = -1;
            $data = $this->Jobseeker->read(null,$id);
            if( !empty($data) )
            {
                if($data['Jobseeker']['codes']==$codes)
                {
                    $this->Jobseeker->id = $id;
                    $this->Jobseeker->saveField('status', '1');
                    $this->Session->setFlash('Your account has been activate now'); 
                    $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);               
                }
            }            
        }
        $this->Session->setFlash('Invalid Link');
    }
    
    function edit_account()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',37);
            if(!empty($this->data))
            {
                $this->data['Jobseeker']['id']= $this->Session->read('User.id');

                $this->Jobseeker->recursive = -1;
                $data = $this->Jobseeker->find('count',array('conditions'=>array('or'=>array('Jobseeker.email'=>$this->data['Jobseeker']['email'], 'Jobseeker.id'=>$this->data['Jobseeker']['id'] ))));
                //var_dump($data);
                if($data<=1)
                {
                    if($this->data['Jobseeker']['email']==$this->data['oldemail'])
                    {                  
                        $this->Jobseeker->save($this->data);
                        $this->Session->setFlash(__('Your account has been saved', true));
                    }
                    else
                    {
                        $this->data['Jobseeker']['codes'] = String::uuid();
                        
                        $to = $this->data['Jobseeker']['email'];
                        $subject = "Jobseeker Member Activation";
                        $setting = "Content-Type: text/html;". "\r\n";
                        $setting = "From: admin@ITJobz.com ". "\r\n";
                        $link = "http://www.itjobz.jux.in/jobseekers/verification/".$this->Session->read('User.id')."/".$this->data['Jobseeker']['codes'];
                        $body = " 
                                Please click this link to activate your jobseeker member account<br>
                                $link
                            ";     
                        if( mail($to, $subject, $body, $setting) )
                        {                     
                            $this->data['Jobseeker']['status']=0;
                            $this->Jobseeker->save($this->data);
                            $this->Session->del('User');
                            $this->Session->setFlash(__('Your account has been saved, please verify your email', true));
                        }
                        else
                        { $this->Session->setFlash(__("Cannot send the email, so you can't change your email", true)); }
                    }
                }
                else
                {
                    $this->Session->setFlash(__('There is an error, this email has been used', true));
                }
            }
            if (empty($this->data))
            {
                $this->Jobseeker->recursive = -1;
                $this->data = $this->Jobseeker->read(null, $this->Session->read('User.id'));
            }
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }
    }
    
    function change_password()
    {
        if($this->Session->read('User.type')=='jobseeker')   
        {
            $this->set('choose1',37);
            if(!empty($this->data))
            {
                $this->Jobseeker->recursive = -1;
                $data = $this->Jobseeker->read(null, $this->Session->read('User.id'));
            
                if($data['Jobseeker']['password']==$this->data['Jobseeker']['old_password'])
                { 
                    $this->data['Jobseeker']['id']= $this->Session->read('User.id');
                    $this->Jobseeker->save($this->data);
                    $this->Session->setFlash(__('Your password has been change', true));
                    $this->redirect(array('controller'=>'jobseekers', 'action'=>'edit_account'),null, true); 
                }
                else
                { $this->Session->setFlash(__('Please provide the right value of your old password', true)); }
            }
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }
    }
    
    function personal_detail()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',2);
            if(!empty($this->data))
            {
                $this->Jobseeker->save($this->data);
                $this->Jobseeker->query("UPDATE `resumes` SET modified = '".date("Y-m-d")."' WHERE jobseeker_id=".$this->Session->read('User.id'));
                $this->redirect(array('controller'=>'resumes', 'action'=>'summary'));
            }
            if (empty($this->data))
            {
                // $this->Jobseeker->recursive = -1;
                $this->data = $this->Jobseeker->read(null, $this->Session->read('User.id'));
				// $this->data = $this->Jobseeker->find($this->Session->read('User.id'));
				// $this->data = $this->Jobseeker->find($this->Session->read('User.id'));
            }
			
            $cities = $this->Jobseeker->City->find('list',array('order'=>'City.name')); 
            $religions = $this->Jobseeker->Religion->find('list',array('order'=>'Religion.id'));
            $skills = $this->Jobseeker->Skill->find('list',array('order'=>'Skill.name'));
			// var_dump($this->data);
			$this->set(compact('cities','fields','skills','religions'));
			
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }        
    }
    
    function view_resume()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',1);
            
            $this->Jobseeker->recursive=1;
            $data = $this->Jobseeker->read(null,  $this->Session->read('User.id'));
            $this->set('data',$data);
            //$this->set('layout','resume');
            
            $this->Jobseeker->Resume->Academic->recursive=-1;
            $academic = $this->Jobseeker->Resume->Academic->find('all',array('conditions'=>array('Academic.id'=>$data['Resume']['academic_id'])));
            $this->set('academic',$academic);
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);        
        }        
        
    }               
    
    function login()
    {
        $this->set('choose1',3);
        if( $this->Session->read('User.type')=='jobseeker' )
        { $this->redirect(array('controller'=>'jobseekers', 'action'=>'index'),null, true); }

        if( $this->Session->read('User.type')=='employer' )
        { $this->redirect(array('controller'=>'employers', 'action'=>'index'),null, true); }        
        
        if(!empty($this->data))
        {
            $this->Jobseeker->recursive=-1;
            $username=mysql_real_escape_string($this->data['Jobseeker']['username']);
            $password=mysql_real_escape_string($this->data['Jobseeker']['password']);
            
            $data=$this->Jobseeker->find('first',array('conditions'=>array('Jobseeker.username'=>$username,'Jobseeker.password'=>$password)));
            if( $data )
            {
                
                if($data['Jobseeker']['status']==1)
                {
                    $temp['type']= 'jobseeker';
                    $temp['id']= $data['Jobseeker']['id'];
                    $temp['username']= $data['Jobseeker']['username'];
                    $temp['password']= $data['Jobseeker']['password'];
                
                    $this->Session->write('User',$temp);
                    
                    $data['Jobseeker']['last_login']=date("Y-m-d");
                    $this->Jobseeker->save($data);
                    
                    $this->redirect(array('controller'=>'jobseekers', 'action'=>'index'),null, true);
                }
                else
                {
                      $this->Session->setFlash(__('There is an error, you haven\'t verify your email', true));                 
                }
            }
            else
            {
                $this->Session->setFlash(__('There is an error, invalid username or password', true));
            }
            
        }
    }
    
    function logout()
    {
        $this->Session->del('User');
        $this->redirect(array('controller'=>'jobs', 'action'=>'index'));
    }                                                            
    
function index() 
{
    if($this->Session->read('User.type')=='jobseeker')         
    {
        $this->set('choose1',31);
        
        $this->Jobseeker->recursive=1;
        $data = $this->Jobseeker->read(null, $this->Session->read('User.id'));
        $assistant = $data['Assistant'];
        
        if($assistant['status']!=0)
        {
            $query = "SELECT distinct(jobs.id) FROM `jobs`,`fields_jobs` WHERE fields_jobs.job_id =jobs.id ";
            $query .= " AND fields_jobs.field_id IN ( SELECT assistants_fields.field_id FROM `assistants_fields` WHERE assistants_fields.assistant_id = ".$assistant['id'].")";         
            $query .= " AND jobs.city_id IN ( SELECT assistants_cities.city_id FROM `assistants_cities` WHERE assistants_cities.assistant_id  = ".$assistant['id'].")";         
            $query .= " AND jobs.position LIKE '%".$assistant['keywords']."%'";
            $query .= " AND jobs.academic_id <= ".$assistant['academic_id'];
            $query .= " AND jobs.experience <= ".$assistant['experience'];
            
            $temp = 0;
            if ( $assistant['salary'] >= MIN_SALARY_4  )
            { $temp  = 4; }
            if ( $assistant['salary'] <= MAX_SALARY_3  )
            { $temp  = 3; }
            if ( $assistant['salary'] <= MAX_SALARY_2  )
            { $temp  = 2; }
            if ( $assistant['salary'] <= MAX_SALARY_1  )
            { $temp  = 1; }

            if ( $assistant['negotiable'] == 1  )
            { $query .= " AND ( jobs.salary >= ".$temp." OR jobs.salary = 0 )"; }
            else
            { $query .= " AND ( jobs.salary >= ".$temp." AND jobs.salary <> 0 )"; }
            
            $temp = " AND ( (jobs.parttime = 1 AND jobs.parttime = ".$assistant['parttime'].")";
            $temp .= " OR (jobs.fulltime = 1 AND jobs.fulltime = ".$assistant['fulltime'].")";
            $temp .= " OR (jobs.freelance = 1 AND jobs.freelance = ".$assistant['freelance'].")";
            $temp .= " OR (jobs.contract = 1 AND jobs.contract = ".$assistant['contract'].")";
            $temp .= " OR (jobs.temporary = 1 AND jobs.temporary = ".$assistant['temporary'].") )";
            
            if ( $assistant['parttime'] || $assistant['fulltime'] || $assistant['freelance'] || $assistant['contract'] || $assistant['temporary'] )
            { $query .= $temp; }


            $temp = " AND ( (jobs.entry = 1 AND jobs.entry = ".$assistant['entry'].")";
            $temp .= " OR (jobs.middle = 1 AND jobs.middle = ".$assistant['middle'].")";
            $temp .= " OR (jobs.senior = 1 AND jobs.senior = ".$assistant['senior'].")";
            $temp .= " OR (jobs.top = 1 AND jobs.top = ".$assistant['top'].") )";

            if ( $assistant['entry'] || $assistant['middle'] || $assistant['senior'] || $assistant['top'] )
            { $query .= $temp; }
            
           //echo $query; exit;
            $query2="SELECT cities.name, jobs.id, jobs.position, jobs.created, jobs.salary, jobs.experience, jobs.entry, jobs.middle, jobs.senior, jobs.top, jobs.employer_id, employers.company_name  FROM `jobs`,`employers`,`cities` WHERE jobs.employer_id = employers.id AND cities.id = jobs.city_id AND jobs.status=1 AND jobs.end_date >='".date("Y-m-d")."' AND jobs.id IN (".$query.") ORDER BY jobs.created DESC,jobs.id DESC";
            $jumlah_jobs = count($this->Jobseeker->query($query2));
            $query2 .=" LIMIT 0,5";
            $jobs = $this->Jobseeker->query($query2);
            
            $this->set('jumlah_jobs',$jumlah_jobs);
            $this->set('jobs',$jobs);
        }
        
        $query=" SELECT * FROM `resumes` WHERE resumes.jobseeker_id = ".$this->Session->read('User.id');
        $resume = $this->Jobseeker->query($query);

        $query=" SELECT count(*) FROM `applicants` WHERE applicants.jobseeker_id = ".$this->Session->read('User.id');
        $applicants = $this->Jobseeker->query($query);

        $query=" SELECT count(*) FROM `clipboards` WHERE clipboards.jobseeker_id = ".$this->Session->read('User.id');
        $clipboards = $this->Jobseeker->query($query);

        
        $this->set('resume',$resume[0]);
        $this->set('assistant',$assistant);
        $this->set('applicants',$applicants[0][0]['count(*)']);
        $this->set('clipboards',$clipboards[0][0]['count(*)']);
        $this->set('email',$data['Jobseeker']['email']);
        
        $this->set('channel', array('title' => 'ITjobz.com')); 
        
    }
    else
    {
        $this->Session->setFlash(__('Please login first', true));
        $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
    }
}    

    function jobalert()
    {
            $this->Jobseeker->recursive=1;
            $data = $this->Jobseeker->read(null, $this->Session->read('User.id'));
            $assistant = $data['Assistant'];
            
            if($assistant['status']!=0)
            {
                $query = "SELECT distinct(jobs.id) FROM `jobs`,`fields_jobs` WHERE fields_jobs.job_id =jobs.id ";
                $query .= " AND fields_jobs.field_id IN ( SELECT assistants_fields.field_id FROM `assistants_fields` WHERE assistants_fields.assistant_id = ".$assistant['id'].")";         
                $query .= " AND jobs.city_id IN ( SELECT assistants_cities.city_id FROM `assistants_cities` WHERE assistants_cities.assistant_id  = ".$assistant['id'].")";         
                $query .= " AND jobs.position LIKE '%".$assistant['keywords']."%'";
                $query .= " AND jobs.academic_id <= ".$assistant['academic_id'];
                $query .= " AND jobs.experience <= ".$assistant['experience'];
                
                $temp = 0;
                if ( $assistant['salary'] >= MIN_SALARY_4  )
                { $temp  = 4; }
                if ( $assistant['salary'] < MAX_SALARY_3  )
                { $temp  = 3; }
                if ( $assistant['salary'] < MAX_SALARY_2  )
                { $temp  = 2; }
                if ( $assistant['salary'] < MAX_SALARY_1  )
                { $temp  = 1; }

                if ( $assistant['negotiable'] != 1  )
                { $query .= " AND ( jobs.salary >= ".$temp." OR jobs.salary = 0 )"; }
                
                $temp = " AND ( (jobs.parttime = 1 AND jobs.parttime = ".$assistant['parttime'].")";
                $temp .= " OR (jobs.fulltime = 1 AND jobs.fulltime = ".$assistant['fulltime'].")";
                $temp .= " OR (jobs.freelance = 1 AND jobs.freelance = ".$assistant['freelance'].")";
                $temp .= " OR (jobs.contract = 1 AND jobs.contract = ".$assistant['contract'].")";
                $temp .= " OR (jobs.temporary = 1 AND jobs.temporary = ".$assistant['temporary'].") )";
                
                if ( $assistant['parttime'] || $assistant['fulltime'] || $assistant['freelance'] || $assistant['contract'] || $assistant['temporary'] )
                { $query .= $temp; }


                $temp = " AND ( (jobs.entry = 1 AND jobs.entry = ".$assistant['entry'].")";
                $temp .= " OR (jobs.middle = 1 AND jobs.middle = ".$assistant['middle'].")";
                $temp .= " OR (jobs.senior = 1 AND jobs.senior = ".$assistant['senior'].")";
                $temp .= " OR (jobs.top = 1 AND jobs.top = ".$assistant['top'].") )";

                if ( $assistant['entry'] || $assistant['middle'] || $assistant['senior'] || $assistant['top'] )
                { $query .= $temp; }
                
                
                $query2="SELECT jobs.id, jobs.desc ,jobs.position, jobs.created, jobs.salary, jobs.experience, jobs.entry, jobs.middle, jobs.senior, jobs.top, employers.company_name, cities.name FROM `jobs`,`employers`,`cities` WHERE jobs.employer_id = employers.id AND jobs.status=1 AND cities.id = jobs.city_id AND jobs.end_date >=".date("Y-m-d")." AND jobs.id IN (".$query.") ORDER BY jobs.created DESC,jobs.id DESC";
                $jobs = $this->Jobseeker->query($query2);
                
            }
        
        $i=0;
        $temp = array();
        foreach($jobs as $job)
        {
            $temp[$i]['position']=$job['jobs']['position'];
            $temp[$i]['id']=$job['jobs']['id'];           
            
            
            $datevariable = date($job['jobs']['created']); 
            $stringArray = explode("-", $datevariable);
            $date = mktime(0,0,0,$stringArray[1],$stringArray[2]+1,$stringArray[0]); 
            $temp[$i]['created']=$date;
            $temp[$i]['desc']=$job['jobs']['desc'];
            $temp[$i]['exp']=$job['jobs']['experience'];
            
            if($job['jobs']['salary']==0)
            { $temp[$i]['salary']=SALARY_0; } 
            if($job['jobs']['salary']==1)
            { $temp[$i]['salary']=SALARY_1; } 
            if($job['jobs']['salary']==2)
            { $temp[$i]['salary']=SALARY_2; }   
            if($job['jobs']['salary']==3)
            { $temp[$i]['salary']=SALARY_3; }  
            if($job['jobs']['salary']==4)
            { $temp[$i]['salary']=SALARY_4; }
            
            if($job['jobs']['entry']==1)
            { $temp[$i]['level']='Entry Level'; }
            if($job['jobs']['middle']==1)
            { $temp[$i]['level']='Middle'; }
            if($job['jobs']['senior']==1)
            { $temp[$i]['level']='Senior'; }
            if($job['jobs']['top']==1)
            { $temp[$i]['level']='Top'; }
            
            $temp[$i]['company_name']=$job['employers']['company_name'];
            $temp[$i]['location']=$job['cities']['name'];
            
            $i++; 
        }
            
        $this->set('jobs',$temp);
        $channel = array(
                        'title' => "ITjobz.com",
                        'link'=>'www.ITjobz.com',
                        'url'=>'www.ITjobz.com',
                        'description'=> 'Job alert for '.$data['Jobseeker']['username']
                    );
        $this->set('channel',$channel);
    }
    
    function index_admin()  
    {
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout='ajax';
        }
        else
        {
            $this->Session->setFlash('You are forbidden for this page');
            $this->redirect(array('controller'=>'jobs', 'action'=>'login_admin'),null, true);
        }             
    }
 
    function filter_admin()  
    {
        $this->layout='ajax';
        if(!empty($this->data['category']))
        {
            $query = "SELECT jobseekers.*,cities.name FROM `jobseekers`,`cities` WHERE jobseekers.city_id = cities.id ";
            if($this->data['category']==2)
            { $query.=" AND jobseekers.status = 0"; }
            $mydate = date("Y-m-d", mktime(0, 0, 0, date('m')-6, date('d'), date('Y')));
            if($this->data['category']==3)
            { $query.=" AND jobseekers.status = 0 AND jobseekers.created < '".$mydate."'"; }
            if($this->data['category']==4)
            { $query.=" AND jobseekers.last_login < '".$mydate."'"; }
            
            if($this->data['id']!='')
            { $query.= " AND jobseekers.id=".$this->data['id']; }
            if($this->data['name']!='')
            { $query.= " AND ( jobseekers.first_name LIKE '%".$this->data['name']."%' OR jobseekers.last_name LIKE '%".$this->data['name']."%' )";}
            $jobseekers = $this->Jobseeker->query($query);
            $this->set('jobseekers',$jobseekers);
        }
        
        if(empty($this->data))
        {
            $query = "SELECT jobseekers.*,cities.name FROM `jobseekers`,`cities` WHERE jobseekers.city_id = cities.id ";
            $jobseekers = $this->Jobseeker->query($query);
            $this->set('jobseekers',$jobseekers);
        }
		// echo $query;
    } 
    
    function edit_admin( $id = null )
    {
        if(!empty($this->data))
        {
            $this->Jobseeker->save($this->data);
            $this->redirect(array('controller'=>'jobseekers','action'=>'filter_admin'));    
        }
        else
        {
            $this->layout = 'ajax';
            $this->Jobseeker->recursive=-1;
            $this->data = $this->Jobseeker->read(null,$id);
        }
    }

    function delete_admin($id)
    {  
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            $this->Jobseeker->del($id);
            $this->redirect(array('controller'=>'jobseekers','action'=>'filter_admin'));
        }        
    }
}
?>
